package com.wsh.sys.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsh.dto.sys.MenuDto;
import com.wsh.sys.MenuService;
import com.wsh.entity.sys.MenuEntity;
import com.wsh.mapper.sys.MenuDao;
import com.wsh.utils.PageUtils;
import com.wsh.vo.sys.MenuTreeVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 王思涵
 * @Date 2019/7/21
 * @Version 1.0
 * @Desciption
 **/
@Service
public class MenuServiceImpl extends ServiceImpl<MenuDao, MenuEntity> implements MenuService {
    @Override
    public List<MenuEntity> getListByParentId(String parentId, boolean parentList) {
        List<MenuEntity> menuEntities = baseMapper.getMenuList(parentId);
        if (parentList) { return menuEntities; }
        for (MenuEntity menuEntity : menuEntities) {
            // 递归
            List<MenuEntity> menuList = baseMapper.getMenuList(menuEntity.getMenuId());
            menuEntity.setChildren(menuList);
        }
        return menuEntities;
    }

    @Override
    public IPage<MenuEntity> getList(MenuDto menuDto) {
        // 分页数据处理
        IPage<MenuEntity> pages = PageUtils.getPage(menuDto);

        // 分页查询处理
        pages.setRecords(baseMapper.getMenuTree("0"));

        return pages;
    }

    @Override
    public boolean deleteMenuTree(String menuId) {
        List<String> menus = new ArrayList<>();
        for (MenuEntity menuEntity : getListByParentId(menuId, true)) {
            menus.add(menuEntity.getMenuId());
        }
        menus.add(menuId);
        return removeByIds(menus);
    }

    @Override
    public List<MenuEntity> getMenuInRouterGroupChildren(String[] menus) {
        HashMap<String,Object> params = new HashMap<String,Object>();
        params.put("menus", menus);
        return baseMapper.getMenuInRouter(params);
    }

    @Override
    public List<MenuTreeVo> getRoleTree(String parentId) {
        return baseMapper.getRoleTree(parentId);
    }


}
